using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using log4net;
using System.Security.Cryptography;
//using NFine.Code;

namespace GZ.Projects.WCS_SVC
{
	public class HttpClientHY
	{
		//private ILog logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
		public HttpClient()
		{
		}
			/// <summary>
		/// http client
		/// </summary>
		/// <param name="type">请求类型:get,post</param>
		/// <param name="param"></param>
		/// <param name="url"></param>
		/// <param name="errorMsg"></param>
		/// <param name="statusCode"></param>
		/// <returns></returns>
		public string Call(string type, string param, string url, out string errorMsg, out int statusCode)
    {
        statusCode = 0;
        errorMsg = string.Empty;
        try
        {
            HttpWebRequest request = WebRequest.CreateHttp(url) as HttpWebRequest;
            request.Method = type.ToUpper();
            request.ContentType = "application/json";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;
            
            if (request.Method == "POST" || request.Method == "PUT")
            {
                byte[] byts = Encoding.UTF8.GetBytes(param);
                request.ContentLength = byts.Length;
                using (Stream s = request.GetRequestStream())
                {
                    s.Write(byts, 0, byts.Length);
                }
            }
            
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                statusCode = (int)response.StatusCode;
                using (Stream s = response.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(s))
                    {
                        string json = sr.ReadToEnd();
                        return json; 
                    }
                }
            }
        }
        catch (WebException ex)
        {
            string text = string.Empty;
            if (ex.Response is HttpWebResponse errorResponse) 
            {
                statusCode = (int)errorResponse.StatusCode;
                if (ex.Status == WebExceptionStatus.ProtocolError && errorResponse.GetResponseStream() != null)
                {
                    using (Stream data = errorResponse.GetResponseStream())
                    using (var reader = new StreamReader(data))
                    {
                        text = reader.ReadToEnd();
                    }
                }
            }
            else if (ex.Status == WebExceptionStatus.Timeout)
            {
                statusCode = (int)HttpStatusCode.RequestTimeout; 
            }
            else 
            {
	            statusCode = (int)HttpStatusCode.ServiceUnavailable;
            }
            errorMsg = ex.Message;
            return text;
        }
        catch (Exception e) 
        {
            errorMsg = e.Message;
            statusCode = (int)HttpStatusCode.InternalServerError; // 500
            return string.Empty;
        }
    }
	}

}